package com.woyou.ui.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import android.app.ProgressDialog;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiItemDetail;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.amap.api.services.poisearch.PoiSearch.OnPoiSearchListener;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.citaq.ideliver.R;
import com.woyou.BroadcastConstant;
import com.woyou.bean.Info;
import com.woyou.bean.MyAddress;
import com.woyou.service.LocationLoopService;
import com.woyou.service.NetWorkCenter;
import com.woyou.ui.adapter.AddrSearchResultAdapter;
import com.woyou.ui.adapter.DefaultSearchAdapter;
import com.woyou.ui.api.RetryNetwork;
import com.woyou.ui.fragment.HintView;
import com.woyou.ui.fragment.ShopsFragment_;
import com.woyou.utils.CharCheckUtil;
import com.woyou.utils.LogUtil;

/**
 * 地址搜索的碎片
 * 
 * @author nsz 2015年1月22日
 */
@EActivity(R.layout.fm_address_search)
public class AddrSearchActivity extends BaseActivity implements RetryNetwork{
	
	private static final String TAG = "AddrSearchActivity";
	
	@ViewById(R.id.left) View left;    						//返回按钮
	@ViewById(R.id.addrsearch_edit) EditText searchEd; 		//输入编辑框
	@ViewById(R.id.search_delete_all) TextView delete;  	//删除按钮
	@ViewById(R.id.keysList) ListView keysList;   			//有历史记录的list
	@ViewById(R.id.keysList_addr) ListView addrKeysList;  	//搜索出结果的list
	@ViewById(R.id.scroll) View keysViewScroll;   
	@ViewById(R.id.scroll_addr) View addrSearchScroll;
	@ViewById(R.id.hint_view) HintView hintView;  			//鉴权失败时出现的
	@ViewById(R.id.addr_search) TextView addrSearch;        //搜索按钮

	@ViewById(R.id.addrsearch_history_rl) RelativeLayout historyRl; // 历史记录
	@ViewById(R.id.addrsearch_history_tv) TextView historyTv;
	
	@ViewById(R.id.addr_search_all_rl) RelativeLayout allHistoryMark; // 清除所有记录的蒙板
	@ViewById(R.id.addrsearch_clear) RelativeLayout allHistoryData; // 是否确定清除历史记录
	@ViewById(R.id.addrsearch_confirm) TextView confirm;
	@ViewById(R.id.addrsearch_cancel) TextView cancel;
	
	@ViewById(R.id.addr_search_item_rl) RelativeLayout itemHistoryMark;	//清除记录的蒙板
	@ViewById(R.id.addrsearch_clear_item) RelativeLayout itemHistoryData;  //清除单条记录
	@ViewById(R.id.confirm_item) TextView confirmItem;
	@ViewById(R.id.cancel_item) TextView cancelItem;
	
	private List<MyAddress> keys = new ArrayList<MyAddress>();
	private List<MyAddress> addrSearchResultKeys = new ArrayList<MyAddress>();
	private View firstKeyItem;  // 第一条定位记录
	private MyAddress firstTag; // 第一个定位地址
	private String inputSearchkeys;
	
	private DefaultSearchAdapter keysAdapter;
	private AddrSearchResultAdapter addrKetsAdapter;
	
	// point 的Timer
	private int point = 0;
	private Timer pointTimer = null;
	private TimerTask pointTimerTask = null;
	private boolean isLocation = false;  //判断是否重新定位
	
	@AfterViews void init(){
		mNetWorkCenter.setRetryNetwork(this);
		hintView.findViewById(R.id.try_again).setOnClickListener(this);
		left.setOnClickListener(this);
		delete.setOnClickListener(this);
		confirm.setOnClickListener(this);
		cancel.setOnClickListener(this);
		historyTv.setOnClickListener(this);
		confirmItem.setOnClickListener(this);
		cancelItem.setOnClickListener(this);
		addrSearch.setOnClickListener(this);
		allHistoryMark.setOnClickListener(this);
		itemHistoryMark.setOnClickListener(this);
		
		addrKetsAdapter = new AddrSearchResultAdapter(addrSearchResultKeys, mContext);
		addrKeysList.setAdapter(addrKetsAdapter);
		
		//显示第一次的定位地址
		show();
		initClickEvent();
		refreshKeys();
	}
	
	private MyAddress remove = null; // 需要移除的那条地址
	/**
	 * 初始化点击事件
	 */
	private void initClickEvent() {
		keysList.setOnItemLongClickListener(new OnItemLongClickListener() {
			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
				if ( allHistoryData.getVisibility() == View.GONE && itemHistoryData.getVisibility() == View.GONE ) {
					remove = keys.get(position - 1);
					itemHistoryData.setVisibility(View.VISIBLE);
					hideSoftInput(mContext, searchEd);
					itemHistoryData.setOnClickListener(new OnClickListener() {
						@Override
						public void onClick(View v) {
							itemHistoryData.setVisibility(View.GONE);
						}
					});
				}
				return false;
			}
		});
		
		keysList.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if ( allHistoryData.getVisibility() == View.GONE && itemHistoryData.getVisibility() == View.GONE) {
					MyAddress myAddress = keys.get(position - 1);
					onSearch(myAddress);
					
				} 
			}
		});
		
		addrKeysList.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if ( allHistoryData.getVisibility() == View.GONE && itemHistoryData.getVisibility() == View.GONE) {
					MyAddress myAddress = addrSearchResultKeys.get(position);
					onSearch(myAddress);
					
				}
			}
		});
		
		searchEd.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
			}
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				inputSearchkeys = s.toString();
				if( inputSearchkeys.length() >  0 ) {
					delete.setVisibility(View.VISIBLE);
					addrSearch.setVisibility(View.VISIBLE);
					// startSearch();  // 输入信息的回调方法
				} else {
					delete.setVisibility(View.GONE);
					addrSearch.setVisibility(View.GONE);
					hintView.setVisibility(View.GONE);
					keysViewScroll.setVisibility(View.VISIBLE);
					addrSearchScroll.setVisibility(View.GONE);
				}
			}
		});
	}
	
	/**
	 * 刷新关键字列表 
	 */
	public void refreshKeys(){
		List<MyAddress> adreessKeys = mUserModel.getAdreessKeys();
		keys.addAll(adreessKeys);
		if ( keys != null ){
			keysAdapter.notifyDataSetChanged(); 
			keysList.setSelection(0);
			if (keys != null && keys.size() > 0 ){
				historyRl.setVisibility(View.VISIBLE); //出现清除按钮
			} else {
				historyRl.setVisibility(View.GONE);
			}
		}

	}
	
	/**
	 * 第一个是定位当前地址
	 */
	private void addFirstItem() {
		firstKeyItem  = View.inflate(mContext, R.layout.item_address_search_keys, null);
		firstKeyItem.findViewById(R.id.location_img).setVisibility(View.VISIBLE);
		keyText = (TextView)firstKeyItem.findViewById(R.id.key_text);
		if( firstTag == null ){
			executeAnim(keyText);
		}else{
			initFirstItem(firstTag);
		}
		keysList.addHeaderView(firstKeyItem);
		firstKeyItem.setOnClickListener(this);
		keysAdapter = new DefaultSearchAdapter(keys, mContext);
		keysList.setAdapter(keysAdapter);
	}
	
	/**
	 * 显示地址搜索结果
	 */
	public void refreshAddressList(){
		if ( addrSearchResultKeys != null && addrSearchResultKeys.size()>0 ) {
			addrSearchScroll.setVisibility(View.VISIBLE);
			keysViewScroll.setVisibility(View.GONE);
			addrKetsAdapter.notifyDataSetChanged();
			addrKeysList.setSelection(0);
		}
	}
	
	/**
	 * 清空历史记录
	 */
	public void clearKeys(){
		keys.clear();
		mUserModel.putAddressKeys(keys);
		refreshKeys();
	}
	
	/**
	 * 搜索与地址相关的商家
	 */
	@SuppressWarnings("rawtypes")
	public void onSearch(MyAddress addr ) {
		hideSoftInput(mContext, searchEd);
		final MyAddress key = addr ;
		
		if(keys == null){
			keys = new ArrayList<MyAddress>();
		}
		if(!containsAddr(keys, key)){
			keys.add(0, key);
			mUserModel.putAddressKeys(keys);
			refreshKeys();
		}else{
			keys.remove(key);
			keys.add(0, key);
			mUserModel.putAddressKeys(keys);
			refreshKeys();
		}
		
		//保存手动定位地址
		LocationLoopService.getInstance(mContext).setChosenAddr(key);
		
		// 发送地址搜索事件
		Intent intent = new Intent(BroadcastConstant.SEND_CHANGE_ADDRESS);
		intent.putExtra(BroadcastConstant.ADDR_PARAMS, addr);
		sendBroadcast(intent);
		
		Info<Class> info = new Info<Class>(ShopsFragment_.class);
		info.setOrigin(ShopsSearchActivity_.class);
		openActivity(HomeActivity_.class, info);
		finish();
		
	}
	
	/**
	 * 搜索地址
	 * @param addr
	 * @param jump
	 */
	private void searchAddress(String addr, final boolean jump){
        final String searchKey = addr;
		showProgressDialog("正在搜索...", true);
        
		poiSearch(searchKey, new OnPoiSearchListener() {
			
			@Override
			public void onPoiSearched(PoiResult result, int rCode) {
				dismissProgressDialog();
				if(rCode == 0){
			        // 搜索POI的结果
			        if (result != null && result.getPois()!=null) {
			        	List<PoiItem> searchAddress = result.getPois();
						if( searchAddress != null ){
							hintView.setVisibility(View.GONE);
							addrSearchResultKeys.addAll(PoiItem2MyAddress(searchAddress));
						}
						
						if( searchAddress == null ){
							noAddr();
							return;
						}
						searchOnResult(jump);
			        }else{
			        	noAddr();
			        }
				}else if(rCode == 31){
					errorAddr("搜索地址出错");
				}else if(rCode == 32){
					errorAddr("key 鉴权失败");
				}
				else if(rCode == 23){
					errorAddr("搜索地址出错:连接超时");
				}
				else if(rCode == 21){
					errorAddr("搜索地址出错:IO 操作异常");
				}
				else if(rCode == 22){
					errorAddr("搜索地址出错:连接异常");
				}
				else if(rCode == 24){
					errorAddr("搜索地址出错:无效的参数");
				}
				else if(rCode == 25){
					errorAddr("搜索地址出错:空指针异常");
				}
				else if(rCode == 26){
					errorAddr("搜索地址出错:url 异常");
				}
				else if(rCode == 27){
					errorAddr("搜索地址出错:网络不给力");
				}
				else if(rCode == 28){
					errorAddr("搜索地址出错:服务器连接失败");
				}
				else if(rCode == 29){
					errorAddr("搜索地址出错:协议解析错误");
				}
				else if(rCode == 30){
					errorAddr("搜索地址出错:http 连接失败");
				}
			}
			
			@Override
			public void onPoiItemDetailSearched(PoiItemDetail arg0, int arg1) {
				
			}
		});
				
	}
	
	@Override
	public void onClick(View v) { 
		switch (v.getId()) {
		case R.id.try_again:
			if ( allHistoryData.getVisibility() == View.GONE && itemHistoryData.getVisibility() == View.GONE) {
				startSearch();
			} 
			break;
		case R.id.search_delete_all:
			if ( allHistoryData.getVisibility() == View.GONE && itemHistoryData.getVisibility() == View.GONE) {
				hideSoftInput(mContext, searchEd);
				if (keys != null && keys.size() > 0 ){
					historyRl.setVisibility(View.VISIBLE); //出现清除按钮
				} else {
					historyRl.setVisibility(View.GONE);
				}
				searchEd.setText("");
				hintView.setVisibility(View.GONE);
				addrSearchScroll.setVisibility(View.GONE);
				keysViewScroll.setVisibility(View.VISIBLE);
			}
			break;
			
		case R.id.left:
			backOperate();
			break;
			
		case R.id.addr_search:
			hideSoftInput(mContext, searchEd);
			inputSearchkeys = searchEd.getText().toString();
			
			if ( allHistoryData.getVisibility() == View.GONE && itemHistoryData.getVisibility() == View.GONE){
				if ( !TextUtils.isEmpty(inputSearchkeys) && !CharCheckUtil.isEmpty(inputSearchkeys) ) {
					startSearch();
				} else {
					showToast("湿主，洒家还不知道您要搜什么?");
				}
			} 
			break;
			
		case R.id.keys_item:
			if ( isLocation ) {
				if ( keyText!=null ) {
					executeAnim(keyText);
				}
				getLocation();
			} else {
				onSearch(firstTag);
			}
			break;
			
		case R.id.addrsearch_confirm:  
			// 确定
			clearKeys();
			allHistoryData.setVisibility(View.GONE);
			break;
		case R.id.addrsearch_cancel: 
			// 取消
			allHistoryData.setVisibility(View.GONE);
			break;
			
		case R.id.addrsearch_history_tv:  
			// 清除历史记录
			if ( allHistoryData.getVisibility() == View.GONE && itemHistoryData.getVisibility() == View.GONE) {
				hideSoftInput(mContext, searchEd);
				allHistoryData.setVisibility(View.VISIBLE);
				allHistoryData.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						allHistoryData.setVisibility(View.GONE);
					}
				});
			}
			break;
			
		case R.id.confirm_item:
			if ( remove != null ) {
				keys.remove(remove);
				keysAdapter.notifyDataSetChanged();
				if ( keys.size() == 0  ){
					historyRl.setVisibility(View.GONE);
				}
				itemHistoryData.setVisibility(View.GONE);
			}
			break;
			
		case R.id.cancel_item:
			itemHistoryData.setVisibility(View.GONE);
			break;
		case R.id.addr_search_item_rl:
			itemHistoryData.setVisibility(View.GONE);
			break;
		case R.id.addr_search_all_rl:
			allHistoryData.setVisibility(View.GONE);
			break;
		default:
			break;
		}
		
	}

	/**
	 * 开始搜索
	 */
	private void startSearch() {     
		if( historyRl.getVisibility() == View.VISIBLE ) {
			historyRl.setVisibility(View.GONE);
		}
		addrSearchResultKeys.clear();
		inputSearchkeys = searchEd.getText().toString();
		if( TextUtils.isEmpty(inputSearchkeys) ){
			showToast("无效关键字");
			return;
		}
		searchAddress(inputSearchkeys, true);
	}
	
	/**
	 * 比对两个地址的经纬度和名称是否相同
	 * @param list
	 * @param addr
	 * @return
	 */
	public boolean containsAddr(List<MyAddress> list, MyAddress addr){
		return list.contains(addr);
	}
	
	public ProgressDialog getProgressDialog(String msg) {
		ProgressDialog progressDialog = new ProgressDialog(mContext);
		progressDialog.setMessage(msg);
		progressDialog.setCancelable(true);
		return progressDialog;
	}
	
	protected boolean isLocationReturn = false;
	private TextView keyText;
	/**
	 * 显示第一次的定位地址
	 */
	public void show() {
		if( firstKeyItem != null ){
			if ( keyText == null ) {
				keyText = (TextView)firstKeyItem.findViewById(R.id.key_text);
			}
			executeAnim(keyText);
		}else{
			addFirstItem();  			//初始化
		}
		hintView.setVisibility(View.GONE);
		addrSearchScroll.setVisibility(View.GONE);
		keysViewScroll.setVisibility(View.VISIBLE);
		getLocation();
	}
	
	/**
	 * 定位地址...的动画
	 * @param keyText
	 */
	private void executeAnim(final TextView keyText) {
		if ( firstKeyItem!=null ) {
			firstKeyItem.setEnabled(false);
		}
		if(pointTimer!=null){
			pointTimer.cancel();
			pointTimer=null;
		}
		if(pointTimerTask!=null){
			pointTimerTask.cancel();
			pointTimerTask=null;
		}
		pointTimer = new Timer();
		pointTimerTask = new TimerTask() {
			@Override
			public void run() {
				runOnUI(new Runnable() {
					public void run() {
						if (point == 0) {
							keyText.setText("正在定位");
							point = 1;
						} else if (point == 1) {
							keyText.setText("正在定位.");
							point = 2;
						} else if (point == 2) {
							keyText.setText("正在定位..");
							point = 3;
						} else {
							keyText.setText("正在定位...");
							point = 0;
						}
					}
				});
			}
		};
		pointTimer.schedule(pointTimerTask, 0, 1000);
	}
	
	private  MyLocitionListener locationListener;
	private void getLocation() {
		try {
			if( locationListener == null ){
				locationListener = new MyLocitionListener();
				mBaiduLocationService.registerLocationListenner(locationListener);
			}

			mBaiduLocationService.startLocation();// 开始定位
			// timeOutTimer.schedule(new locationTimeOutTask(), 15000);//15秒超时
		} catch (Exception e) {
			if(e!=null)
			e.printStackTrace();
		}
		
	}
	
	class MyLocitionListener implements BDLocationListener{

		@Override
		public void onReceiveLocation(BDLocation location) {
			mBaiduLocationService.stop();
			if ( !NetWorkCenter.isNetworkConnected(mActivity) ) {  		//断网
				if(pointTimer!=null){ 			
					pointTimer.cancel();
					pointTimer=null;
				}
				if(pointTimerTask!=null){
					pointTimerTask.cancel();
					pointTimerTask=null;
				}
				runOnUI( new Runnable() {
					public void run() {
						keyText.setText("定位失败（未连接互联网）");
						firstKeyItem.setEnabled(true);
						isLocation = true;
						return;
					}
				});
			}
			
			if ( location!=null ){
				if(pointTimer!=null){
					pointTimer.cancel();
					pointTimer=null;
				}
				if(pointTimerTask!=null){
					pointTimerTask.cancel();
					pointTimerTask=null;
				}
				if( firstKeyItem!=null ){
					firstKeyItem.setEnabled(true);
					if ( !TextUtils.isEmpty(location.getAddrStr()) ){
						keyText.setText(location.getAddrStr());
						isLocation = false;
					} else {
						if ( NetWorkCenter.isNetworkConnected(mActivity) ){
							keyText.setText("无法获取地址名称");
						} else {
							keyText.setText("定位失败（点此继续定位）");
						}
						isLocation = true;
					}
					
				}
			}
			
			mBaiduLocationService.stop();
			timeOutTimer.cancel();
	        isLocationReturn  = true;
	        
	        MyAddress addr = transform(location); 
			initFirstItem(addr);
			firstTag = addr;
		}
	}

	//取上一次地址
	@UiThread void lastAddress() {
		if(pointTimer!=null){
			pointTimer.cancel();
			pointTimer=null;
		}
		if(pointTimerTask!=null){
			pointTimerTask.cancel();
			pointTimerTask=null;
		}
		BDLocation location = mBaiduLocationService.getLastLocation();
		if ( location != null ) {
			MyAddress address = transform(location);
			if ( keyText == null ) {
				keyText = (TextView) firstKeyItem.findViewById(R.id.key_text);
			}
			keyText.setText(address.getAddrName());
			firstKeyItem.setTag("上次定位:" + address.getAddrName());
		}
	}

    /**
     * 对象转换
     * @param location
     * @return
     */
	private MyAddress transform(BDLocation location) {
		MyAddress addr = new MyAddress();
		addr.setAddrName(location.getAddrStr());
		addr.setCityName(location.getCity());
		addr.setLat(location.getLatitude()+"");
		addr.setLng(location.getLongitude()+"");
		return addr;
    }

    @UiThread void initFirstItem(MyAddress myAddress) {
		if( firstKeyItem!=null ){
			if ( keyText == null ){
				keyText = (TextView)firstKeyItem.findViewById(R.id.key_text);
			}
			if ( !"".equals(myAddress.getAddrName()) && myAddress.getAddrName() != null ) {
				if(pointTimer!=null){
					pointTimer.cancel();
					pointTimer=null;
				}
				if(pointTimerTask!=null){
					pointTimerTask.cancel();
					pointTimerTask=null;
				}
				keyText.setText(myAddress.getAddrName());
				firstKeyItem.setTag(myAddress);
			} 
		}
	}

    /**
     * 正确搜索，返回搜索结果
     * @param jump
     */
	private void searchOnResult(final boolean jump) {
		refreshAddressList();
		hintView.setVisibility(View.GONE);
		if( addrSearchResultKeys.size()>0 ||!jump){
			refreshAddressList();
			addrSearchScroll.setVisibility(View.VISIBLE);
			keysViewScroll.setVisibility(View.GONE);
		}else 
		if(addrSearchResultKeys.size()>0){
			MyAddress address = addrSearchResultKeys.get(0);
			onSearch(address);
		}else if( addrSearchResultKeys.size()== 0 ){
			noAddr();
		}
	}
	
	/**
	 * 搜索失败，无相关地址
	 */
	private void noAddr() {
		hintView.setVisibility(View.VISIBLE);
		hintView.setHint("呃...没有找到相关地址");
		hintView.disTryAgain();
		refreshAddressList();
		addrSearchScroll.setVisibility(View.VISIBLE);
		keysViewScroll.setVisibility(View.GONE);
	}
	
	/**
	 * 定位错误，出现鉴权失败
	 * @param hint
	 */
	private void errorAddr(String hint){
		hintView.setVisibility(View.VISIBLE);
		hintView.setHint(hint);
		hintView.openButton();
		refreshAddressList();
		addrSearchScroll.setVisibility(View.VISIBLE);
		keysViewScroll.setVisibility(View.GONE);
	}
	
	private Timer timeOutTimer = new Timer();
	class locationTimeOutTask extends TimerTask{
		@Override
		public void run() {  
			if(!isLocationReturn){
				if( mBaiduLocationService!=null ){
					mBaiduLocationService.stop();
				}
				runOnUI(new Runnable() {
					public void run() {
						firstKeyItem.setEnabled(true);
						if ( keyText == null ) {
							keyText = (TextView) firstKeyItem.findViewById(R.id.key_text);
						}
						keyText.setText("定位失败（点此继续定位）");
						isLocation = true;
					}
				});
				
			}
			
		}
		
	}
	
	/**
	 * 搜索兴趣点
	 * @param address
	 * @param listener
	 */
	public void poiSearch(String address, OnPoiSearchListener listener){
		String city = mUserModel.getUserInfo().getCity().getCity();
		if( TextUtils.isEmpty(city) ){
			city = mBaiduLocationService.getLastLocation().getCity();
		}
		// 第一个参数表示搜索字符串，第二个参数表示POI搜索类型，二选其一
		// 第三个参数表示POI搜索区域的编码，必设
		PoiSearch.Query query = new PoiSearch.Query(address, "", city);
		query.setPageSize(20);// 设置每页最多返回多少条poiitem
		query.setPageNum(0);//设置查第一页
		PoiSearch poiSearch = new PoiSearch(mContext, query);
		poiSearch.setOnPoiSearchListener(listener);
		poiSearch.searchPOIAsyn();
	}
	
	/**
	 * 对象转换
	 * @param res
	 * @return
	 */
	public List<MyAddress> PoiItem2MyAddress(List<PoiItem> res){
		List<MyAddress> list = new ArrayList<MyAddress>();
		for(PoiItem addr: res){
			MyAddress ma = new MyAddress();  
			LogUtil.d(TAG,"adName:"  + addr.getAdName());
			LogUtil.d(TAG,"cityName:"  + addr.getCityName());
			LogUtil.d(TAG,"title:"  + addr.getTitle());
			LogUtil.d(TAG,"direction:"  + addr.getDirection());
			LogUtil.d(TAG,"distance:"  + addr.getDistance());
			LogUtil.d(TAG,"snippet:"  + addr.getSnippet());
			ma.setAddrName(addr.getTitle());
			ma.setCityName(addr.getCityName()); 
			ma.setStreet(addr.getSnippet());
			ma.setLat(addr.getLatLonPoint().getLatitude()+"");
			ma.setLng(addr.getLatLonPoint().getLongitude()+""); 
			list.add(ma);
		}
		return list;
	}
	
	
	
	@Override
	public void retry() {
		getLocation();
	}
	
	@Override
	public void netError() {
		
	}

	@SuppressWarnings("rawtypes")
	@Override
	public boolean backOperate() {
		hideSoftInput(mContext, searchEd);
		if ( allHistoryData.getVisibility() == View.VISIBLE || itemHistoryData.getVisibility() == View.VISIBLE){
			allHistoryData.setVisibility(View.GONE);
			itemHistoryData.setVisibility(View.GONE);
			return true;
		}  else {
			if( addrSearchScroll.getVisibility() == View.VISIBLE ){
				addrSearchScroll.setVisibility(View.GONE);
				keysViewScroll.setVisibility(View.VISIBLE);
				searchEd.setText("");
			} 
			Info<Class> info = new Info<Class>(ShopsFragment_.class);
			info.setOrigin(ShopsSearchActivity_.class);
			openActivity(HomeActivity_.class, info);
			finish();
			return true;
			
		}
		
		
	}
	
	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		// 拦截确定按钮事件
		if( event.getKeyCode() == KeyEvent.KEYCODE_ENTER ){
			inputSearchkeys = searchEd.getText().toString();
				if ( !TextUtils.isEmpty(inputSearchkeys) && !CharCheckUtil.isEmpty(inputSearchkeys) ) {
					hideSoftInput(mContext, searchEd);
					startSearch();
				} else {
					showToast("湿主，洒家还不知道您要搜什么?");
				}
			return true;
		}
		return super.dispatchKeyEvent(event);
	}
	

}
